package com.ckd.flyingtrip.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ZoomControls;

import com.baidu.location.BDAbstractLocationListener;
import com.baidu.location.BDLocation;
import com.baidu.location.LocationClient;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.model.LatLng;
import com.ckd.flyingtrip.R;
import com.ckd.flyingtrip.utils.Tools;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MyServiceActivity extends BaseActivity {

    @BindView(R.id.bmapView)
    MapView mMapView;
    @BindView(R.id.cd_title_img)
    ImageView cdTitleImg;
    @BindView(R.id.cd_close)
    TextView cdClose;
    @BindView(R.id.cd_title_btn)
    TextView cdTitleBtn;
    @BindView(R.id.drawerlayout_main_layout)
    LinearLayout drawerlayoutMainLayout;
    @BindView(R.id.btn_dingwei)
    LinearLayout btnDingwei;
    @BindView(R.id.txt_name)
    TextView txtName;
    @BindView(R.id.txt_phone)
    TextView txtPhone;
    @BindView(R.id.fl_station_info)
    LinearLayout flStationInfo;
    @BindView(R.id.lin_city)
    LinearLayout linCity;
    @BindView(R.id.cehualin)
    LinearLayout cehualin;

    private LocationClient mLocationClient;
    private boolean changeCity = false;
    private BaiduMap mBaiduMap;
    private BDLocation mBDLocationBean;
    private boolean is_mBDLocationBean;
    private LatLng currLatLng;
    private LatLng mapCenterLatLng;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.service_activity);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        Tools.title_info(MyServiceActivity.this,"服务站");
        initMapBaidu();
        Tools.initMapLocation(mLocationClient, new MyLocationListener());
    }

    /*
     * 初始化地图配置
     * */
    private void initMapBaidu() {
        mMapView.showZoomControls(false);
        mMapView.showScaleControl(false);
        mBaiduMap = mMapView.getMap();

        mBaiduMap.setOnMapLoadedCallback(new BaiduMap.OnMapLoadedCallback() {
            @Override
            public void onMapLoaded() {
                MapView.setMapCustomEnable(true);
            }
        });
        // 隐藏logo
        View child = mMapView.getChildAt(1);
        if (child != null && (child instanceof ImageView || child instanceof ZoomControls)) {
            child.setVisibility(View.INVISIBLE);
        }
        mBaiduMap.setMyLocationEnabled(true);
    }


    @Override
    protected void onResume() {
        mMapView.onResume();
        super.onResume();
    }

    @Override
    protected void onPause() {
        mMapView.onPause();
        super.onPause();
    }

    @OnClick({R.id.btn_my, R.id.btn_shuaxin, R.id.btn_dingwei, R.id.btn_shaoma, R.id.btn_kefu, R.id.btn_zuche})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_my:
                startAction(MyselfCenterActivity.class);
                break;
            case R.id.btn_shuaxin:
                break;
            case R.id.btn_dingwei:
                toast("正在定位中 请稍等...");
//                定位
                if (mLocationClient != null) {
                    is_mBDLocationBean = false;
                    mLocationClient.requestLocation();
                }
                break;
            case R.id.btn_shaoma:
                break;
            case R.id.btn_kefu:
                break;
            case R.id.btn_zuche:
                break;
        }
    }

    @OnClick(R.id.btn_dingwei)
    public void onClick() {
    }

    /**
     * 实现定位回调
     */
    public class MyLocationListener extends BDAbstractLocationListener {
        @Override
        public void onReceiveLocation(BDLocation location) {
            //此处的BDLocation为定位结果信息类，通过它的各种get方法可获取定位相关的全部结果
            //以下只列举部分获取经纬度相关（常用）的结果信息
            //更多结果信息获取说明，请参照类参考中BDLocation类中的说明
            if (location != null) {
                if (changeCity) {
                    changeCity = false;
                    return;
                }
                if (location.getLongitude() < 10 || location.getLatitude() < 10) {
                    return;
                }
                MyLocationData locData = new MyLocationData.Builder()
                        .accuracy(location.getRadius())
                        // 此处设置开发者获取到的方向信息，顺时针0-360
                        .direction(location.getDirection())
                        .latitude(location.getLatitude())
                        .longitude(location.getLongitude()).build();
                mBaiduMap.setMyLocationData(locData);
                Log.d("qyy", "lat" + (int) location.getLatitude());
                Log.d("qyy", "lng" + (int) location.getLongitude());
                mBDLocationBean = location;
                LocChangeAction(mBDLocationBean, true);

            }
        }
    }


    private void LocChangeAction(BDLocation mBDLocationBean,
                                 boolean isNeedAnim) {
        try {
            if (!is_mBDLocationBean) {
                // 移动到 我的位置
                currLatLng = new LatLng(mBDLocationBean.getLatitude(),
                        mBDLocationBean.getLongitude());
                MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(currLatLng);
                if (isNeedAnim) {
                    mBaiduMap.animateMapStatus(u);
                }
            }
            is_mBDLocationBean = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

